using Decal.Adapter;
using Decal.Adapter.Wrappers;
using Decal.Interop.Inject;
using System;

namespace SixthSense
{
    [View("SixthSense.ViewXML.Schema.xml")]
    [WireUpControlEvents]

    public partial class PluginCore
    {
        private void InitMainView()
        {
            PopupCheckbox.Checked = _Config.CurrentOptions.Popup;
            ClipboardCheckbox.Checked = _Config.CurrentOptions.Clipboard;
            SoundCheckbox.Checked = _Config.CurrentOptions.Sound;
            ChatWindowCheckbox.Checked = _Config.CurrentOptions.ChatWindow;
            RelativeCoordsCheckbox.Checked = _Config.CurrentOptions.RelativeCoords;
            IgnoreCorpseCheckbox.Checked = _Config.CurrentOptions.IgnoreCorpses;
            IgnoreHookCheckbox.Checked = _Config.CurrentOptions.IgnoreHooks;

            // Items
            ByValueCheckbox.Checked = _Config.CurrentOptions.TrackValue;
            ItemValueEdit.Text = _Config.CurrentOptions.MinValue.ToString();
            ByRatioCheckbox.Checked = _Config.CurrentOptions.TrackRatio;
            ItemRatioEdit.Text = _Config.CurrentOptions.Ratio.ToString();
            PortalsCheckbox.Checked = _Config.CurrentOptions.TrackPortals;

            UpdateChoosers();
        }

        [ControlReference("CoordsText")]        private StaticWrapper CoordsText;
        [ControlReference("Notebook")]          private NotebookWrapper Notebook;

        //
        // Items Tab
        //
        [ControlReference("ItemList")]          private ListWrapper ItemList;

        //
        // Monsters Tab
        //
        [ControlReference("MonsterList")]       private ListWrapper MonsterList;

        //
        // Special Tab
        //
        [ControlReference("SpecialList")]       private ListWrapper SpecialList;

        //
        // Config Tab
        //
        //[ControlReference("ConfigBook")]        private NotebookWrapper ConfigBook;

        //
        // Config > Profiles Tab
        //
        [ControlReference("ProfileChooser")]    private ChoiceWrapper ProfileChooser;
        [ControlReference("ProfileNameEdit")]   private TextBoxWrapper ProfileNameEdit;

        //
        // Config > Options Tab
        //
        [ControlReference("PopupCheckbox")]         private CheckBoxWrapper PopupCheckbox;
        [ControlReference("ClipboardCheckbox")]     private CheckBoxWrapper ClipboardCheckbox;
        [ControlReference("SoundCheckbox")]         private CheckBoxWrapper SoundCheckbox;
        [ControlReference("ChatWindowCheckbox")]    private CheckBoxWrapper ChatWindowCheckbox;
        [ControlReference("RelativeCoordsCheckbox")]        private CheckBoxWrapper RelativeCoordsCheckbox;
        [ControlReference("IgnoreCorpseCheckbox")]  private CheckBoxWrapper IgnoreCorpseCheckbox;
        [ControlReference("IgnoreHookCheckbox")]    private CheckBoxWrapper IgnoreHookCheckbox;

        //
        // Config > Items Tab
        //
        [ControlReference("ItemChooser")]           private ChoiceWrapper ItemChooser;
        [ControlReference("ByValueCheckbox")]       private CheckBoxWrapper ByValueCheckbox;
        [ControlReference("ItemValueEdit")]         private TextBoxWrapper ItemValueEdit;
        [ControlReference("ByRatioCheckbox")]       private CheckBoxWrapper ByRatioCheckbox;
        [ControlReference("ItemRatioEdit")]         private TextBoxWrapper ItemRatioEdit;
        [ControlReference("PortalsCheckbox")]       private CheckBoxWrapper PortalsCheckbox;

        [ControlEvent("Notebook", "Change")]
        private void Notebook_Change(object sender, IndexChangeEventArgs e)
        {
        }

        //
        // Items Tab
        //
        [ControlEvent("ItemList", "Selected")]
        private void ItemList_Selected(object sender, ListSelectEventArgs e)
        {
            WriteToChat("ItemList_Selected " + _Items[e.Row].ID);
            Core.Actions.CurrentSelection = _Items[e.Row].ID;
        }

        //
        // Monsters Tab
        //
        [ControlEvent("MonsterList", "Selected")]
        private void MonsterList_Selected(object sender, ListSelectEventArgs e)
        {
        }

        //
        // Special Tab
        //
        [ControlEvent("SpecialList", "Selected")]
        private void SpecialList_Selected(object sender, ListSelectEventArgs e)
        {
        }

        //
        // Config Tab
        //
        [ControlEvent("textbook", "Change")]
        private void textbook_Change(object sender, IndexChangeEventArgs e)
        {
        }

        //
        // Config > Profiles Tab
        //
        [ControlEvent("ProfileHelpButton", "Click")]
        private void ProfileHelpButton_Click(object sender, ControlEventArgs e)
        {
            try
            {
                WriteToChat("  Sixth Sense Profile Information", 21);
                WriteToChat("  Profiles allow you to have several lists of items you wish to track, and to switch between them quickly without having to add / remove individual items.", 8);
                WriteToChat(" *  Delete: ", 3);
                RawWriteToChat("Deletes the currently selected profile.  Not reversible!", 13);
                WriteToChat(" *  Rename: ", 3);
                RawWriteToChat("Renames the currently selected profile to the name entered.", 13);
                RawWriteToChat(" *  New: ", 3);
                RawWriteToChat("Creates a copy of the currently selected profile using the name entered.", 13);
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        [ControlEvent("ProfileChooser", "Change")]
        private void ProfileChooser_Change(object sender, IndexChangeEventArgs e)
        {
        }

        [ControlEvent("ProfileNameEdit", "End")]
        private void ProfileNameEdit_End(object sender, TextBoxEndEventArgs e)
        {
        }

        [ControlEvent("ProfileDeleteButton", "Click")]
        private void ProfileDeleteButton_Click(object sender, ControlEventArgs e)
        {
            try
            {
                // not allowed to delete default, yo!
                WriteToChat("Comparing " + ProfileChooser.Text[ProfileChooser.Selected] + " to Default");
                if (ProfileChooser.Text[ProfileChooser.Selected] != "Default")
                {
                    // if it's not "Default" delete it and load default. Gotta have something for a profile
                    WriteToChat("Deleting " + ProfileChooser.Text[ProfileChooser.Selected]);
                    _Config.Profiles.Remove(_Config.Profiles.FindByName(ProfileChooser.Text[ProfileChooser.Selected]));
                    WriteToChat("Loading default profile");
                    _Config.LoadDefaultProfile();

                    WriteToChat("Loaded profile: " + _Config.CurrentProfile.Name);
                    WriteToChat("Removing ProfileChooser Index:" + ProfileChooser.Selected.ToString());
                    // clean up the profile chooser
                    ProfileChooser.Remove(ProfileChooser.Selected);

                    WriteToChat("ProfileChooser.Count " + ProfileChooser.Count.ToString());
                    for (int i = 0; i < ProfileChooser.Count; i++)
                    {
                        WriteToChat("Comparing " + ProfileChooser.Text[i] + " to " + _Config.CurrentProfile.Name);
                        if (ProfileChooser.Text[i] == _Config.CurrentProfile.Name)
                        {
                            WriteToChat("Found");
                            ProfileChooser.Selected = i;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteToChat("Exception (ProfileDeleteButton_Click): " + ex.Message);
            }
        }

        [ControlEvent("ProfileRenameButton", "Click")]
        private void ProfileRenameButton_Click(object sender, ControlEventArgs e)
        {
            if (ProfileNameEdit.Text == "")
            {
                WriteToChat("You must enter a new name for the profile!");
            }
            else
            {
                // renaming the profile to whatever may be there. good luck!
                try
                {
                    // first, test if the profile name already exists
                    Profile nameTest = _Config.Profiles.FindByName(ProfileNameEdit.Text);
                    if (nameTest != null)
                    {
                        WriteToChat("A profile already exists with that name!");
                        return;
                    }

                    if (_Config.CurrentProfile.Name == "Default")
                    {
                        // not allowed to rename default.  Have to clone it
                        Profile pNew = _Config.CurrentProfile.Copy();
                        pNew.Name = ProfileNameEdit.Text;
                        _Config.Profiles.Add(pNew);
                        _Config.CurrentProfile = _Config.Profiles.FindByName(ProfileNameEdit.Text);
                        ProfileChooser.Add(ProfileNameEdit.Text, 1);
                    }
                    else
                    {
                        // new name, not default.

                        // clean up the profile chooser box before I lose the old name
                        for (int i = 0; i < ProfileChooser.Count; i++)
                        {
                            if (ProfileChooser.Text[i].ToString() == _Config.CurrentProfile.Name)
                            {
                                ProfileChooser.Remove(i);
                            }
                        }

                        _Config.CurrentProfile.Name = ProfileNameEdit.Text;
                        ProfileChooser.Add(ProfileNameEdit.Text, 1);
                    }

                    // set the active profile option to the new one we just renamed
                    _Config.CurrentOptions.ActiveProfile = _Config.CurrentProfile.Name;

                    // and update the chooser UI
                    UpdateChoosers();

                    _Config.CommitToDisk();
                }
                catch (Exception ex)
                {
                    WriteToChat("Exception (ProfileNameEdit_End): " + ex.Message);
                }
            }
        }

        [ControlEvent("ProfileNewButton", "Click")]
        private void ProfileNewButton_Click(object sender, ControlEventArgs e)
        {
            if (ProfileNameEdit.Text == "")
            {
                WriteToChat("You must enter a new name for the profile!");
            }
            else
            {
                Profile pNew = new Profile();
                pNew.Name = ProfileNameEdit.Text;

                // can't enter a dupe (TODO have class prevent this)
                Profile pTest = _Config.Profiles.FindByName(pNew.Name);
                if (pTest != null)
                {
                    // name found, no can do
                    WriteToChat("There is already a profile with that name!");
                }
                else
                {
                    _Config.Profiles.Add(pNew);
                    _Config.CurrentProfile = _Config.Profiles.FindByName(ProfileNameEdit.Text);
                    ProfileChooser.Add(ProfileNameEdit.Text, 1);

                    // set the active profile option to the new one we just made
                    _Config.CurrentOptions.ActiveProfile = _Config.CurrentProfile.Name;

                    // and update the chooser UI
                    UpdateChoosers();

                    _Config.CommitToDisk();
                }
            }
        }

        //
        // Config > Options Tab
        //
        [ControlEvent("OptionsHelpButton", "Click")]
        private void OptionsHelpButton_Click(object sender, ControlEventArgs e)
        {
        }

        [ControlEvent("PopupCheckbox", "Change")]
        private void PopupCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            try
            {
                _Config.CurrentOptions.Popup = PopupCheckbox.Checked;
                _Config.CommitToDisk();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        [ControlEvent("ClipboardCheckbox", "Change")]
        private void ClipboardCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            try
            {
                _Config.CurrentOptions.Clipboard = ClipboardCheckbox.Checked;
                _Config.CommitToDisk();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        [ControlEvent("SoundCheckbox", "Change")]
        private void SoundCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            try
            {
                _Config.CurrentOptions.Sound = SoundCheckbox.Checked;
                _Config.CommitToDisk();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        [ControlEvent("ChatWindowCheckbox", "Change")]
        private void ChatWindowCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            try
            {
                _Config.CurrentOptions.ChatWindow = ChatWindowCheckbox.Checked;
                _Config.CommitToDisk();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        [ControlEvent("RelativeCoordsCheckbox", "Change")]
        private void RelativeCoordsCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            try
            {
                _Config.CurrentOptions.RelativeCoords = RelativeCoordsCheckbox.Checked;
                _Config.CommitToDisk();

                //UpdateItemLocations();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        [ControlEvent("IgnoreCorpseCheckbox", "Change")]
        private void IgnoreCorpseCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            try
            {
                _Config.CurrentOptions.IgnoreCorpses = IgnoreCorpseCheckbox.Checked;
                _Config.CommitToDisk();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        [ControlEvent("IgnoreHookCheckbox", "Change")]
        private void IgnoreHookCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            try
            {
                _Config.CurrentOptions.IgnoreHooks = IgnoreHookCheckbox.Checked;
                _Config.CommitToDisk();
            }
            catch (Exception ex)
            {
                errorLogging.LogError(errorLogFile, ex);
            }
        }

        //
        // Config > Items Tab
        //
        [ControlEvent("ItemsHelpButton", "Click")]
        private void ItemsHelpButton_Click(object sender, ControlEventArgs e)
        {
        }

        [ControlEvent("ItemChooser", "Change")]
        private void ItemChooser_Change(object sender, IndexChangeEventArgs e)
        {
        }

        [ControlEvent("ItemDeleteButton", "Click")]
        private void ItemDeleteButton_Click(object sender, ControlEventArgs e)
        {
            WriteToChat("ItemDeleteButton_Click event");
        }

        [ControlEvent("ItemEditButton", "Click")]
        private void ItemEditButton_Click(object sender, ControlEventArgs e)
        {
            WriteToChat("ItemEditButton_Click event");
        }

        [ControlEvent("ItemNewButton", "Click")]
        private void ItemNewButton_Click(object sender, ControlEventArgs e)
        {
            WriteToChat("ItemNewButton_Click event");
            _EditView.Activate();
        }

        [ControlEvent("ByValueCheckbox", "Change")]
        private void ByValueCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            _Config.CurrentOptions.TrackValue = e.Checked;
            WriteToChat("_Config.CurrentOptions.TrackValue = " + _Config.CurrentOptions.TrackValue);
        }

        [ControlEvent("ItemValueEdit", "End")]
        private void ItemValueEdit_End(object sender, TextBoxEndEventArgs e)
        {
            _Config.CurrentOptions.MinValue = Convert.ToInt32(ItemValueEdit.Text);
            WriteToChat("_Config.CurrentOptions.MinValue = " + _Config.CurrentOptions.MinValue.ToString());
        }

        [ControlEvent("ByRatioCheckbox", "Change")]
        private void ByRatioCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            _Config.CurrentOptions.TrackRatio = e.Checked;
            WriteToChat("_Config.CurrentOptions.TrackRatio = " + _Config.CurrentOptions.TrackRatio);
        }

        [ControlEvent("ItemRatioEdit", "End")]
        private void ItemRatioEdit_End(object sender, TextBoxEndEventArgs e)
        {
            _Config.CurrentOptions.Ratio = Convert.ToInt32(ItemRatioEdit.Text);
            WriteToChat("_Config.CurrentOptions.Ratio = " + _Config.CurrentOptions.Ratio.ToString());
        }

        [ControlEvent("PortalsCheckbox", "Change")]
        private void PortalsCheckbox_Change(object sender, CheckBoxChangeEventArgs e)
        {
            _Config.CurrentOptions.TrackPortals = e.Checked;
            WriteToChat("_Config.CurrentOptions.TrackPortals = " + _Config.CurrentOptions.TrackPortals);
        }

        private void UpdateChoosers()
        {
            ProfileChooser.Clear();
            foreach (Profile p in _Config.Profiles)
            {
                ProfileChooser.Add(p.Name, 0);
            }

            // set the UI to choose the active profile
            for (int i = 0; i < ProfileChooser.Count; i++)
            {
                if (ProfileChooser.Text[i] == _Config.CurrentProfile.Name)
                {
                    ProfileChooser.Selected = i;
                }
            }

            // Items
            ItemChooser.Clear();
            foreach (AlertInfo item in _Config.CurrentProfile.Alerts)
            {
                ItemChooser.Add(item.Name, 1);
            }

            if (ItemChooser.Selected < 0)
            {
                ItemChooser.Selected = 0;
            }
        }
    }
}

